package com.itheima.health.service.impl;

import com.itheima.health.dao.MemberDao;
import com.itheima.health.pojo.Member;
import com.itheima.health.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class MemberServiceImpl  implements MemberService {
    @Autowired
    private MemberDao memberDao;
    @Transactional
    @Override
    public Member createOrUpdate(Member member) {
        log.info("[会员-添加]data:{}",member);
        Member pojo = memberDao.selectByPhoneNumber(member.getPhoneNumber());
        if (pojo != null){
            //会员已经存在了
            member.setId(pojo.getId());
            memberDao.updateByIdSelective(member);
        }else {
            //会员不存在
            member.setRegTime(new Date());
            memberDao.insert(member);
        }
        return memberDao.selectById(member.getId());
    }
    @Override
    public List<Integer> findMemberCountMonths(List<String> mouths) {
        List<Integer> memberCount = new ArrayList<>();
        for (String mouth : mouths) {
            String date = mouth+".31";
            Integer count =memberDao.findMemberCountBeforeData(date);
            memberCount.add(count);
        }
        return memberCount;
    }
}
